//variables where we store our html elements
var $inputTags;
var $inputTagsCaption;


var $inputUrl;
var $inputUrlCaption;

var $inputTitle;
var $inputTitleCaption;

var $inputTriggerFeedQuery;
var $inputTriggerFeedQueryCaption;

var $outputFeedback;
var $outputFeedbackCaption;

//misc
var currentConfig;
var currentConsole;

var currentUrl;
var currentTitle;

$(document).ready(function() { 

    loadConfig();

    loadJqueryHandlers();

    loadJqueryMisc();

    if (currentConfigStage != "standalone") {
        //        injectContentScript();
        //        currentConsole.log("sending request to content script...");
        //        sendRequestToContentScript(); 

        getCurrentTabInfo();
    }
    else {
        //        currentUrl="http://stackoverflow.com/questions/4845032/wheres-the-official-jsp-tutorial";
        currentUrl="http://free.domain.name/en.html?domain=doesntexist.com";
        triggersBookmarksReadByUrlQuery();
    }
});


/****************************FUNCTION FOR LOGGING**************************************
 ** 
 ********************************************************************************
 */

function markMyWebLog(logMsg) {
    $outputFeedback.text(logMsg);
}



/****************************MISC **************************************
 ** 
 **
 ********************************************************************************
 */

function uiAddTag(tag) {
    $(".current-tags-container").append('<span class="tag-container" tag="'+tag+'">'
        +tag+' <img class="tag-container-close-icon" src="images/close2.png" '
        +'title="Remove this tag for this URL" /></span>');
    
}

function uiDeleteTag(tag) {
    currentConsole.log("jsonQueries.js onBookmarksDeleteByUrlAndTagQuerySuccess : removing UI for tag '"+tag+"'");
    $(".tag-container[tag='"+tag+"']").remove();
}

function getCurrentTabInfo() {
    chrome.tabs.getSelected(null, function(tab) {
        currentConsole.log("getting info for the tab ...");        

        currentUrl=tab.url;
        currentTitle=tab.title;

        $inputUrl.val(currentUrl);
        $inputTitle.val(currentTitle);
        
        //we have to put the call here, since this function uses currentUrl ...
        triggersBookmarksReadByUrlQuery();
    });
}
 
/**** DEPRECATED : WE DONT NEED CONTENT SCRIPTS JUST TO GET THE INFO 
 *    RETRIEVED IN FUNCTION getCurrentTabInfo()!
 * 
 */
function injectContentScript() {
    currentConsole.log("injecting content script...");
    chrome.tabs.executeScript(null, {
        file: "scripts/jquery-1.5.2.js"
    }, function(){
        chrome.tabs.executeScript(null, {
            file: "scripts/contentScript.js"
        });
    });
    
    
// thanks goes to serg for the code above
// http://stackoverflow.com/questions/4976996/chromes-tabs-executescript-passing-parameters-and-using-libraries

}

function sendRequestToContentScript() {
    
    chrome.tabs.getSelected(null, function(tab) {
        currentConsole.log("requesting infos from content script ...");        
        chrome.tabs.sendRequest(tab.id, {
            nothing:"nothing"
        }, function(response) {
            onResponse(response);
        });
    });
}

function onResponse(response) {
    currentConsole.log("response received from the content script: "+response['URL']+" and "+response['title']);
    
    $inputUrl.val(response['URL']);
    $inputTitle.val(response['title']);
    
    //we end the conversation
    sendResponse({}); 
}
